import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { getDeptList, getPostList, getRoleList } from '/@/api/backs/system/dict';
import moment from 'moment';
export const columns: BasicColumn[] = [
  {
    title: '名字',
    dataIndex: 'nickname',
  },
  {
    title: '账号',
    dataIndex: 'username',
  },
  {
    title: '邮箱',
    dataIndex: 'email',
  },
  {
    title: '手机号',
    dataIndex: 'mobile',
  },
  {
    title: '部门',
    dataIndex: 'dept.name',
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 100,
    slots: { customRender: 'status' },
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    customRender: ({ record }) => {
      const time = record.createTime;
      return time ? moment(time).format('YYYY-MM-DD') : '-';
    },
  },
  {
    title: '最后登录IP',
    dataIndex: 'loginIp',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'nickname',
    label: '名字',
    component: 'Input',
    colProps: { span: 6 },
    componentProps: ({ tableAction }) => {
      return {
        onPressEnter: () => {
          const { reload } = tableAction;
          reload();
        },
      };
    },
  },
  {
    field: 'mobile',
    label: '手机号',
    component: 'Input',
    colProps: { span: 6 },
    componentProps: ({ tableAction }) => {
      return {
        onPressEnter: () => {
          const { reload } = tableAction;
          reload();
        },
      };
    },
  },
  {
    field: 'status',
    label: '状态',
    colProps: { span: 4 },
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '启用',
          value: 0,
        },
        {
          label: '禁用',
          value: 1,
        },
      ],
    },
  },
  {
    field: 'createTime',
    label: '创建时间',
    component: 'RangePicker',
    colProps: { span: 8 },
    componentProps: {
      valueFormat: 'X',
      showTime: true,
    },
  },
];
const isDisabled = (type: string) => type === 'edit';
export const formSchema: FormSchema[] = [
  {
    field: 'username',
    label: '账号',
    component: 'Input',
    required: true,
  },
  {
    field: 'password',
    label: '密码',
    component: 'InputPassword',
    required: true,
    // dynamicDisabled:({ values}) => {
    //   return isDisabled(values.propsType)
    // }
    ifShow: ({ values }) => !isDisabled(values.propsType),
  },
  {
    field: 'nickname',
    label: '名字',
    required: true,
    component: 'Input',
  },

  {
    field: 'deptId',
    label: '部门',
    required: true,
    component: 'ApiTreeSelect',
    componentProps: {
      api: () => getDeptList({ status: 0 }),
      replaceFields: {
        title: 'name',
        key: 'id',
        value: 'id',
      },
    },
  },

  {
    field: 'mobile',
    label: '手机号',
    required: true,
    component: 'InputNumber',
    componentProps:{
      maxlength:11
    },
    rules: [
      {
        required: true,
        validator: async (rule, value) => {
          const phoneReg = /^1[34578]\d{9}$$/;
          if (!phoneReg.test(value) && value) {
            return Promise.reject('请输入正确的手机号');
          }
          return Promise.resolve();
        },
        trigger: 'change',
      },
    ],
  },
  {
    field: 'postIds',
    label: '岗位',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: getPostList,
      showSearch: true,
      filterOption: (input, option) => {
        return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
      },
      labelField: 'name',
      valueField: 'id',
    },
  },
  {
    label: '角色',
    field: 'userRoleIdList',
    required: true,
    component: 'ApiSelect',
    componentProps: {
      api: () => getRoleList({}),
      labelField: 'name',
      valueField: 'id',
      mode: 'multiple',
      getPopupContainer: () => document.body,
      showSearch: true,
      filterOption: (input, option) => {
        return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
      },
    },
  },
  {
    field: 'sex',
    label: '性别',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '男',
          value: 0,
        },
        {
          label: '女',
          value: 1,
        },
      ],
    },
  },
  {
    field: 'email',
    label: '邮箱',
    component: 'Input',
  },
];
export const userRoleFormSchema: FormSchema[] = [
  {
    label: '编号',
    field: 'id',
    show: false,
    component: 'Input',
  },
  {
    label: '用户名称',
    field: 'username',
    component: 'Input',
    dynamicDisabled: () => true,
  },
  {
    label: '用户名字',
    field: 'nickname',
    component: 'Input',
    dynamicDisabled: () => true,
  },
];
